package com.bajiao.service.reserve.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.util.DateUtils;
import com.bajiao.entity.reserve.Reserve;
import com.bajiao.mapper.reserve.ReserveMapper;
import com.bajiao.service.reserve.ReserveService;
import com.bajiao.vo.reserve.ReserveResponse;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 订座表
 * @Author: FanJq
 * @Date:   2025-01-13
 * @Version: V1.0
 */
@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve> implements ReserveService {

    /**
     * @Description
     * 创建预定
     * @date 2025/1/13 10:05
     * @author FanJq
     * @param userId
     * @param storeId
     * @return
    */
    @Override
    public void create(String userId, String storeId) {
        Reserve reserve = this.getToday(userId, storeId);
        if (ObjectUtil.isNotNull(reserve))
            throw new DataHandleException("已预订成功");
        reserve = new Reserve();
        reserve.setUserId(userId);
        reserve.setStoreId(storeId);
        reserve.setReserveTime(new Date());
        super.save(reserve);
    }

    /**
     * @Description
     * 获取当天预订单
     * @date 2025/1/13 10:09
     * @author FanJq
     * @param userId
     * @param storeId
     * @return {@link Reserve}
    */
    @Override
    public Reserve getToday(String userId, String storeId) {
        String today = DateUtils.date_sdf.get().format(new Date());
        Reserve reserve = super.lambdaQuery()
                .eq(Reserve::getStoreId, storeId)
                .eq(Reserve::getUserId, userId)
                .apply("DATE_FORMAT(reserve_time, '%Y-%m-%d') = {0}", today)
                .last("LIMIT 1")
                .one();
        return reserve;
    }

    /**
     * @Description
     * 获取店铺预订列表
     * @date 2025/1/13 10:39
     * @author FanJq
     * @param storeId
     * @return {@link List< ReserveResponse>}
    */
    @Override
    public List<ReserveResponse> getReserveList(String storeId, String filterDate) {
        List<ReserveResponse> responseList = super.baseMapper.getReserveList(storeId, filterDate);
        return responseList;
    }
}
